package com.xt.website_manager.controller;

import com.xt.website_manager.po.CategorySecond;
import com.xt.website_manager.po.News;
import com.xt.website_manager.po.NewsExt;
import com.xt.website_manager.po.User;
import com.xt.website_manager.service.CategoryService;
import com.xt.website_manager.service.NewsService;
import com.xt.website_manager.util.MyPageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  NewsController
 * @Date 2019/8/28/11:00
 * @Description TODO:(一句话描述这个类)
 */
@Controller
@RequestMapping("/news")
public class NewsController {
    @Resource
    private HttpSession session;
    @Resource
    private NewsService newsService;
    @Resource
    private CategoryService categoryService;
    /**
     * 跳转新闻页
     */
    @RequestMapping("/toNews.html")
    public String toNews(NewsExt newsExt, Model model, @ModelAttribute("title") String title,@ModelAttribute("pageNow")String pageNow,@ModelAttribute("csid")String csid){
        if(title!=null){
            newsExt.setTitle(title);
        }
        if (pageNow!=null && !"".equals(pageNow.trim())){
            newsExt.setPageNow(Integer.parseInt(pageNow));
        }
        if (csid!=null && !"".equals(csid.trim())){
            newsExt.setCsid(Integer.parseInt(csid));
        }

        //新闻列表
        MyPageInfo<NewsExt> myPageInfo = newsService.findNewsList(newsExt);
        model.addAttribute("myPageInfo",myPageInfo);
        //二级类目
        List<CategorySecond> categorySeconds = categoryService.findCategorySecondByType(1);
        model.addAttribute("categorySeconds",categorySeconds);
        return "news";
    }

    /**
     * 跳转添加新闻页
     * @return
     */
    @RequestMapping("/addNews.html")
    public String addNews(Model model){
        List<CategorySecond> categorySeconds = categoryService.findCategorySecondByType(1);
        model.addAttribute("categorySeconds",categorySeconds);
        return "addNews";
    }

    /**
     * 执行添加新闻
     * @param news
     * @return
     */
    @RequestMapping("/doAdd.htmlNews.html")
    public String doAddNews(News news){
        User user =(User)session.getAttribute("user");
        news.setUid(user.getId());
        newsService.addNews(news);
        return "redirect:/news/toNews.html";
    }

    /**
     * 删除新闻
     */
    @RequestMapping("/delNews.html")
    @ResponseBody
    public String delNews(@RequestParam("nid[]") String[] nid){
        if(nid!=null){
            for (String id : nid) {
                newsService.delNews(Integer.parseInt(id));
            }
        }
        return "success";
    }


    /**
     * 去往修改新闻页面
     */
    @RequestMapping("/toUpdateNews.html")
    public String toUpdateNews(Model model,NewsExt newsExt){
        newsExt =(NewsExt)newsService.findNewsById(newsExt.getId());
        model.addAttribute("newsExt",newsExt);

        List<CategorySecond> categorySeconds = categoryService.findCategorySecondByType(1);
        model.addAttribute("categorySeconds",categorySeconds);
        return "updateNews";
    }
    /**
     * 修改新闻
     */
    @RequestMapping("/updateNews.html")
    public String updateNews(NewsExt newsExt, String selTitle, String selCsid, RedirectAttributes model){
        newsService.updateNews(newsExt);
        System.err.println(selTitle);
        model.addFlashAttribute("title",selTitle);
        model.addFlashAttribute("pageNow",newsExt.getPageNow());
        model.addFlashAttribute("csid",selCsid);

        return "redirect:/news/toNews.html";
    }
}
